const App = getApp();
const md5 = require('../../utils/md5.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    username:'',
    password:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    console.log(options)
    _this.setData({
      options
    });
  },

  /**
   * 授权登录
   */
  login(e) {
    let _this = this;
    wx.getUserProfile({desc:'想获取你的头像等公开信息',success:(res)=>{
      console.log(res)
      wx.setStorageSync('user_info', res.rawData)
        // 跳转回原页面
        e.detail.value.password = md5.hex_md5( e.detail.value.password)
        _this.wxlogin()
        App._get('UserLogin',e.detail.value, (res2) => {
          console.log('登陆获取信息',res2)
          // 跳转回原页面
          wx.setStorageSync('user_id', res2.Data.User.F_UserId);
          wx.setStorageSync('hasFubiaoPromission', res2.Data.hasFubiaoPromission);
          wx.setStorageSync('hasZhuBiaoPromission', res2.Data.hasZhuBiaoPromission);
          wx.setStorageSync('F_Account', res2.Data.User.F_Account);

          _this.onNavigateBack(_this.data.options.delta);
          console.log('登陆成功')
        });
    },fail:(res)=>{
      console.log(res)
    }})

  },

  /**
   * 微信登陆
   */
  wxlogin(){
    wx.login({
      success: res=>{
        console.log('微信登陆code',res.code)
        let that = this;
        wx.request({
          method: 'GET',
          url: App.api_test+"GetOpenId?code="+res.code,
          header: {
            'content-type': 'application/json'
          },
          success (res) {
              if(res.statusCode == 200){
                //已登录
                  wx.setStorageSync('openid', res.data.OpenId)
              }else {
                  //获取openId失败
                  wx.showToast({
                    title: '获取openid失败',
                    icon: 'none',
                    duration: 2000
                  })
              }
          }
      })
      }
    })
  },
  /**
   * 暂不登录
   */
  onNotLogin() {
    let _this = this;
    // 跳转回原页面
    _this.onNavigateBack(_this.data.options.delta);
  },

  /**
   * 授权成功 跳转回原页面
   */
  onNavigateBack(delta) {
    wx.navigateBack({
      delta: Number(delta || 1)
    });
  },

})